package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.biz.entity.PmPowerplantProConBom;
import cn.stylefeng.guns.modular.biz.entity.WmInventoryQty;
import cn.stylefeng.guns.modular.biz.mapper.PmPowerplantProConBomMapper;
import cn.stylefeng.guns.modular.biz.mapper.WmInventoryQtyMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.OutWeightRecodsCoalQuantityRequest;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.OutWeightRecods;
import cn.stylefeng.guns.modular.biz.enums.OutWeightRecodsExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.OutWeightRecodsMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.OutWeightRecodsRequest;
import cn.stylefeng.guns.modular.biz.service.OutWeightRecodsService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 入炉煤分摊记录业务实现层
 * 第一步插入出库记录 第二步扣减少库存、热值、硫份
 * @author 张朋飞
 * @since 2024/11/01 15:56
 */
@Service
public class OutWeightRecodsServiceImpl extends ServiceImpl<OutWeightRecodsMapper, OutWeightRecods> implements OutWeightRecodsService {

    @Resource
    private WmInventoryQtyMapper wmInventoryQtyMapper;//库存记录表
    @Resource
    private PmPowerplantProConBomMapper pmPowerplantProConBomMapper;//投料记录表
	@Override
    public void add(OutWeightRecodsRequest outWeightRecodsRequest) {
        OutWeightRecods outWeightRecods = new OutWeightRecods();

       if(ObjectUtil.isAllNotEmpty(outWeightRecodsRequest.getCoalQuantitys())){
           for (OutWeightRecodsCoalQuantityRequest coalQuantity : outWeightRecodsRequest.getCoalQuantitys()) {
               BeanUtil.copyProperties(outWeightRecodsRequest, outWeightRecods);//复制属性
               String wiqCode=coalQuantity.getWiqCode();//仓位号
               String outWeight=coalQuantity.getOutWeight();//出库量
               if(ObjectUtil.isAllNotEmpty(wiqCode,outWeight)){
                   //第一步插入出库记录
                   outWeightRecods.setWiqCode(wiqCode);
                   outWeightRecods.setWeight(NumberUtil.toBigDecimal(outWeight));
                   String sumQnetar=NumberUtil.mul(outWeight,outWeightRecods.getAvgQnetar()).toString();
                   outWeightRecods.setSumQnetar(sumQnetar); // 总热值
                   String sumStd=NumberUtil.mul(outWeight,outWeightRecods.getAvgStd()).toString();
                   outWeightRecods.setSumStd(sumStd); // 总干基硫分
                   this.save(outWeightRecods);//保存出库记录
                   //第二步扣减少库存、热值、硫份
                   WmInventoryQty wmInventoryQty=wmInventoryQtyMapper.selectOne(new LambdaQueryWrapper<WmInventoryQty>().eq(WmInventoryQty::getWiqCode,wiqCode));
                   wmInventoryQty.setWiqMass(NumberUtil.sub(wmInventoryQty.getWiqMass(),NumberUtil.toBigDecimal(outWeight)));
                   wmInventoryQty.setWiqStd(NumberUtil.sub(wmInventoryQty.getWiqStd(),NumberUtil.toBigDecimal(sumStd)));
                   wmInventoryQty.setWiqQnetar(NumberUtil.sub(wmInventoryQty.getWiqQnetar(),NumberUtil.toBigDecimal(sumQnetar)));
                   wmInventoryQtyMapper.updateById(wmInventoryQty);
                   //第三步更新入炉煤的状态变Y，表示已使用
                   pmPowerplantProConBomMapper.update(new PmPowerplantProConBom(),new LambdaUpdateWrapper<PmPowerplantProConBom>().eq(PmPowerplantProConBom::getGatherDate,outWeightRecods.getGatherDate()).set(PmPowerplantProConBom::getDelFlag,"Y"));
               }
           }
       }
    }

    @Override
    public void del(OutWeightRecodsRequest outWeightRecodsRequest) {
        OutWeightRecods outWeightRecods = this.queryOutWeightRecods(outWeightRecodsRequest);
        this.removeById(outWeightRecods.getOwrId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(OutWeightRecodsRequest outWeightRecodsRequest) {
        this.removeByIds(outWeightRecodsRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(OutWeightRecodsRequest outWeightRecodsRequest) {
        OutWeightRecods outWeightRecods = this.queryOutWeightRecods(outWeightRecodsRequest);
        BeanUtil.copyProperties(outWeightRecodsRequest, outWeightRecods);
        this.updateById(outWeightRecods);
    }

    @Override
    public OutWeightRecods detail(OutWeightRecodsRequest outWeightRecodsRequest) {
        return this.queryOutWeightRecods(outWeightRecodsRequest);
    }

    @Override
    public PageResult<OutWeightRecods> findPage(OutWeightRecodsRequest outWeightRecodsRequest) {
        LambdaQueryWrapper<OutWeightRecods> wrapper = createWrapper(outWeightRecodsRequest);
        Page<OutWeightRecods> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<OutWeightRecods> findList(OutWeightRecodsRequest outWeightRecodsRequest) {
        LambdaQueryWrapper<OutWeightRecods> wrapper = this.createWrapper(outWeightRecodsRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/11/01 15:56
     */
    private OutWeightRecods queryOutWeightRecods(OutWeightRecodsRequest outWeightRecodsRequest) {
        OutWeightRecods outWeightRecods = this.getById(outWeightRecodsRequest.getOwrId());
        if (ObjectUtil.isEmpty(outWeightRecods)) {
            throw new ServiceException(OutWeightRecodsExceptionEnum.OUT_WEIGHT_RECODS_NOT_EXISTED);
        }
        return outWeightRecods;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/11/01 15:56
     */
    private LambdaQueryWrapper<OutWeightRecods> createWrapper(OutWeightRecodsRequest outWeightRecodsRequest) {
        LambdaQueryWrapper<OutWeightRecods> queryWrapper = new LambdaQueryWrapper<>();

        Long owrId = outWeightRecodsRequest.getOwrId();
        queryWrapper.eq(ObjectUtil.isNotNull(owrId), OutWeightRecods::getOwrId, owrId);

        String wiqCode = outWeightRecodsRequest.getWiqCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(wiqCode), OutWeightRecods::getWiqCode, wiqCode);

        BigDecimal weight = outWeightRecodsRequest.getWeight();
        queryWrapper.eq(ObjectUtil.isNotNull(weight), OutWeightRecods::getWeight, weight);

        String sumQnetar = outWeightRecodsRequest.getSumQnetar();
        queryWrapper.like(ObjectUtil.isNotEmpty(sumQnetar), OutWeightRecods::getSumQnetar, sumQnetar);

        String sumStd = outWeightRecodsRequest.getSumStd();
        queryWrapper.like(ObjectUtil.isNotEmpty(sumStd), OutWeightRecods::getSumStd, sumStd);

        String avgQnetar = outWeightRecodsRequest.getAvgQnetar();
        queryWrapper.like(ObjectUtil.isNotEmpty(avgQnetar), OutWeightRecods::getAvgQnetar, avgQnetar);

        String avgStd = outWeightRecodsRequest.getAvgStd();
        queryWrapper.like(ObjectUtil.isNotEmpty(avgStd), OutWeightRecods::getAvgStd, avgStd);


        return queryWrapper;
    }

}
